//
//  ByBottomListPopView.m
//  Homept_ios
//
//  Created by pro on 16/9/1.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByBottomListPopView.h"

@implementation ByBottomListPopView
{
    NSInteger _selectedFirstRow;
    NSInteger _selectedSecondRow;

}

#pragma mark - 视图初始化
- (void)setupUI {
    [super setupUI];
    
    self.contentMode = ByPickerContentModeBottom;
    
    _arrayData = @[];
    
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
}


#pragma mark - setters
//- (void)setArrayData:(NSArray *)arrayData {
//    
//    _arrayData = arrayData;
//    _selectedFirstRow = 0;
//    _selectedSecondRow = 0;
//    [self.pickerView reloadAllComponents];
//}
//#pragma mark - picker delegate
//- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
//    return 2;
//}
//
//- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
//    if (component == 0) {
//        return self.arrayData.count;
//    } else {
//        NSDictionary *dic = self.arrayData[_selectedFirstRow];
//        NSString *key = dic.allKeys.firstObject;
//        NSArray *value = dic[key];
//        return value.count;
//    }
//}
//
//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
//    return 44;
//}
//
//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
//    if (component == 0) {
//        return self.width / 3;
//    } else {
//        return self.width / 3 * 2;
//    }
//}
//
//- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//    if (component == 0) { // 点击的是第一组
//        _selectedFirstRow = row;
//        _selectedSecondRow = 0;
//        [pickerView reloadComponent:1];      // 刷新第二组数据
//    } else {              // 点击的是第二组
//        _selectedSecondRow = row;
//    }
//    
//}
//
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
// 
//    UILabel *label = [[UILabel alloc] init];
//    [label setTextAlignment:NSTextAlignmentCenter];
//    if (component == 0) {
//        NSDictionary *dic = self.arrayData[row];
//        label.text = dic.allKeys.firstObject;
//    } else {
//        NSDictionary *selectedDic = self.arrayData[_selectedFirstRow];
//        NSString *key = selectedDic.allKeys.firstObject;
//        NSArray *value = selectedDic[key];
//        label.text = value[row];
//    }
//    return label;
//}
//
//#pragma mark - 点击
//- (void)selectedOk {
//    
//    NSDictionary *selectedDic = self.arrayData[_selectedFirstRow];
//    NSString *key = selectedDic.allKeys.firstObject;
//    NSArray *value = selectedDic[key];
//    if (self.delegate) {
//        
//        [self.delegate popViewDidSelected:value[_selectedSecondRow]];
//    }
//    if (self.popviewDidSelected) {
//        self.popviewDidSelected(value[_selectedSecondRow]);
//    }
//    [super selectedOk];
//}


- (void)setDataDic:(NSDictionary *)dataDic {
    
    _dataDic = dataDic;
    _selectedFirstRow = 0;
    _selectedSecondRow = 0;
    [self.pickerView reloadAllComponents];

}

#pragma mark - picker delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.dataDic.allKeys.count;
    } else {
        NSString *key = self.dataDic.allKeys[_selectedFirstRow];
        NSArray *value = self.dataDic[key];
        return value.count;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 44;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return self.width / 3;
    } else {
        return self.width / 3 * 2;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) { // 点击的是第一组
        _selectedFirstRow = row;
        _selectedSecondRow = 0;
        [pickerView reloadComponent:1];      // 刷新第二组数据
    } else {              // 点击的是第二组
        _selectedSecondRow = row;
    }
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    UILabel *label = [[UILabel alloc] init];
    [label setTextAlignment:NSTextAlignmentCenter];
    if (component == 0) {
        label.text = self.dataDic.allKeys[row];
    } else {
        NSString *key = self.dataDic.allKeys[_selectedFirstRow];
        NSArray *value = self.dataDic[key];
        label.text = value[row];
    }
    return label;
}

#pragma mark - 点击
- (void)selectedOk {
    
    NSString *key = self.dataDic.allKeys[_selectedFirstRow];
    NSArray *value = self.dataDic[key];
    if (self.delegate) {
        
        [self.delegate popViewDidSelected:value[_selectedSecondRow]];
    }
    if (self.popviewDidSelected) {
        self.popviewDidSelected(value[_selectedSecondRow]);
    }
    if (self.popviewDidSelectedV2) {
        NSString *ret = nil;
        if (value.count != 0) {
            ret = value[_selectedSecondRow];
        }
        self.popviewDidSelectedV2(key, ret);
    }
    [super selectedOk];
}



@end
